package com.example.xiaomengai.service.impl;

import com.example.xiaomengai.entity.Character;
import com.example.xiaomengai.entity.Organization;
import com.example.xiaomengai.entity.RelationDTO;

import com.example.xiaomengai.entity.StrangeFire;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.neo4j.core.Neo4jTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 功能
 * 作者：梦天
 * 日期：2025/4/28 19:38
 */
@Repository
public class Neo4jRepositoryImpl {
    @Autowired
    private Neo4jTemplate neo4jTemplate;

    public Optional<Character> findByName(String name) {
        String query = "MATCH (n:人物) WHERE n.name = $name RETURN n";
        Optional<Character> result = neo4jTemplate.findOne(query, Map.of("name", name), Character.class);

        // 打印结果（调试用）
        result.ifPresentOrElse(
                character -> System.out.println("查询结果: " + character),
                () -> System.out.println("未找到名称为「%s」的势力: " + name)
        );
        return result;
    }
    public Optional<Organization> findOrganizationByName(String organizationName) {
        String query = "MATCH (n:`地区/势力`) WHERE n.name = $name RETURN n";
        Optional<Organization> result = neo4jTemplate.findOne(
                query,
                Map.of("name", organizationName),
                Organization.class
        );

        // 增强版调试打印
        result.ifPresentOrElse(
                org -> System.out.println("查询结果: " + org),
                () -> System.out.printf("未找到名称为「%s」的势力: ", organizationName)
        );

        return result;
    }
    public Optional<StrangeFire> findStrangeFireByName(String name) {
        String query = "MATCH (n:异火) WHERE n.name = $name RETURN n";
        Optional<StrangeFire> result = neo4jTemplate.findOne(
                query,
                Map.of("name", name),
                StrangeFire.class
        );

        result.ifPresentOrElse(
                fire -> System.out.println("查询结果: " + fire),
                () -> System.out.printf("未找到名称为「%s」的异火", name)
        );

        return result;
    }

    public List<RelationDTO> findRelationsBetweenCharacters(String char1Name, String char2Name) {
        String query = "MATCH (c1:人物 {name: $char1Name})-[r]->(c2:人物 {name: $char2Name}) " +
                "RETURN " +
                "c1.name as c1, " +
                "type(r) as relationType, " +
                "c2.name as c2";

        List<RelationDTO> results = neo4jTemplate.findAll(
                query,
                Map.of("char1Name", char1Name, "char2Name", char2Name),
                RelationDTO.class
        );

        // 调试打印
        System.out.printf("查询「%s」和「%s」之间的关系，找到%d条结果%n",
                char1Name, char2Name, results.size());
        results.forEach(r -> System.out.println(
                "关系类型: " + r.getRelationType() +
                        ", c1: " + r.getC1() +
                        ", c2: " + r.getC2()
        ));

        return results;
    }

    public List<RelationDTO> findCharacterRelationsByType(String charName, String relationType) {
        String query = "MATCH (c1:人物 {name: $charName})-[r:`" + relationType + "`]->(c2) " +
                "RETURN " +
                "c1.name as c1, " +
                "type(r) as relationType, " +
                "c2.name as c2 ";

        List<RelationDTO> results = neo4jTemplate.findAll(
                query,
                Map.of("charName", charName),
                RelationDTO.class
        );

        // 调试打印

        return results;
    }
}

